/**
 * 
 */
package cvcg.model;

import java.io.Serializable;
import java.util.Comparator;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.ForeignKey;

/**
 * @author jaracje
 *
 */
@SuppressWarnings("serial")
@Entity
@Table(name="ATTENDINGPLAYER")
public class AttendingPlayer implements Comparator<AttendingPlayer>,Serializable
{
	@Id @GeneratedValue
	private Long id;
	@Temporal(TemporalType.TIMESTAMP)
	private Date joinDate;
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="PLAYER_F")
	@ForeignKey(name = "CTR_ATTENDINGPLAYER_R_PLAYER")
	private Player player;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Date getJoinDate() {
		return joinDate;
	}
	public void setJoinDate(Date joinDate) {
		this.joinDate = joinDate;
	}
	public Player getPlayer() {
		return player;
	}
	public void setPlayer(Player player) {
		this.player = player;
	}
	
	/** 
	 * Comparator defined to specify order from one game session
	 */
	@Override
	@Transient
	public int compare(AttendingPlayer ap1, AttendingPlayer ap2) 
	{			
		if (ap1!=null && ap2!=null && ap1.getJoinDate()!=null && ap2.getJoinDate()!=null) 
		{			
			return ap1.joinDate.compareTo(ap2.getJoinDate());
		}
		return -1;		
	}
	
	@Transient
	public String getPlayerName() 
	{
		String name = "";
		if (player!=null) 
		{
			name = player.getName();
		}
		return name;
	}
	
	/**
	 * @return owned game count from player
	 */
	@Transient
	public Integer getOwnedGameCount() 
	{
		int count = 0;
		if (player!=null) 
		{
			count = player.getOwnedGames().size();
		}
		return Integer.valueOf(count);
	}
	
	@Transient
	public Integer getKnownGameCount() 
	{
		int count = 0;
		if (player!=null) 
		{
			count = player.getRuleMasteredGames().size();
		}
		return Integer.valueOf(count);		
	}
}
